package com.erp.app.catalog.catalogDtl.web;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

import com.erp.app.common.ProjectConstants;
import com.erp.app.common.select.beans.SelectDelegate;
//import com.erp.app.setup.season.web.SeasonForm;
import com.erp.app.util.web.ActionErrorUtil;
import com.erp.framework.controller.form.MultiRow;
import com.erp.framework.controller.form.SimpleValidator;
import com.erp.framework.model.schema.TableSchema;
import com.erp.framework.util.CommonUtil;
import com.erp.framework.util.user.User;

public class CatalogDtlCommonValidator extends SimpleValidator{
	
	public void validateSearch(ActionErrors errors, ActionForm formIn) {

//		SeasonForm form = (SeasonForm)formIn;
//		TableSchema ts = form.getTs();

	}
	
	public void validateSave(ActionErrors errors, ActionForm formIn) {
		CatalogDtlForm form = (CatalogDtlForm) formIn;
		ArrayList catalogDtlList = form.getMultiRows();
		TableSchema ts = form.getTs();	
		
		 for (int i = 0; (catalogDtlList != null) && (i < catalogDtlList.size()); i++) {
			 CatalogDtlRow row = (CatalogDtlRow)catalogDtlList.get(i);
			 if(CommonUtil.isEmpty(row.getHdrDtlType())){
				 ActionErrorUtil.addErrorForGeneralFieldBlank(errors, ts.getDisplayWebLabelByObjectId("LBL_GRID_DTL_TYPE") + " at Line No." + row.getDtlId());
			 }
			 if(CommonUtil.isEmpty(row.getHdrDtlName())){
				 ActionErrorUtil.addErrorForGeneralFieldBlank(errors, ts.getDisplayWebLabelByObjectId("LBL_GRID_DTL_NAME") + " at Line No." + row.getDtlId());
			 }
			 if(CommonUtil.isEmpty(row.getHdrRecursiveTime())){
				 ActionErrorUtil.addErrorForGeneralFieldBlank(errors, ts.getDisplayWebLabelByObjectId("LBL_GRID_RECURSIVE_TIME") + " at Line No." + row.getDtlId());
			 }
			 if(CommonUtil.isEmpty(row.getHdrStatus())){
				 ActionErrorUtil.addErrorForGeneralFieldBlank(errors, ts.getDisplayWebLabelByObjectId("LBL_GRID_STATUS") + " at Line No." + row.getDtlId());
			 }
			 if(!CommonUtil.isEmpty(row.getHdrRecursiveTime()) && !CommonUtil.isValidNumber(row.getHdrRecursiveTime())){
				 ActionErrorUtil.addErrorForInvalidNumber(errors, ts.getDisplayWebLabelByObjectId("LBL_GRID_RECURSIVE_TIME") + " at Line No." + row.getDtlId());
			 }
		 }
	}
	
	public void moreValidateSearch(ActionMapping mapping, ActionForm formIn,
			HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

	}

	public void moreValidateSave(ActionMapping mapping, ActionForm formIn,
			HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

	}
	
	public void validateDelDtlRow(ActionMapping mapping, ActionForm formIn,
			HttpServletRequest request, HttpServletResponse response, ActionErrors errors){
		 CatalogDtlForm form = (CatalogDtlForm) formIn;
		 HttpSession session = request.getSession(false);
		 int pageEventSrc = Integer.parseInt(form.getPageEventSrc());
		 User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);
         CatalogDtlRow row = (CatalogDtlRow) form.getMultiRows(0).get(pageEventSrc);
         SelectDelegate dg = new SelectDelegate();
         String count = (String) dg.selectOneValue("select to_char(count(1)) from t_fabric_catalog_dtl where company_code = '" + row.getCompanyCode() + "' and " +
        		 " dtl_id = '" + row.getDtlId() + "' ");
         if(!"0".equals(count)){
        	 ActionErrorUtil.addErrorForSpecficPurpose(errors, " Unable to detele Line No. " + row.getDtlId() + ", cause there are Fabrics mapped to this Detail.");
         }
         count = (String) dg.selectOneValue("select to_char(count(1)) from t_catalog_security_field where company_code = '" +  row.getCompanyCode() + "' and " +
        		 " dtl_id = " + row.getDtlId() + "  and USER_CODE = '" + editingUser.getUserId() + "' " );
         if(!"0".equals(count)){
        	 ActionErrorUtil.addErrorForSpecficPurpose(errors, " Unable to detele Line No.  " + row.getDtlId() + ", cause there are Catalog Security mapped to this Detail.");
         }
         
		
	}
	
}
